﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace Omega.Game.Framework
{
    static public class DataManager
    {

        static public void Save(string filename, object objectToSerialize)
        {
            Stream stream = null;
            BinaryFormatter bFormatter;
            try
            {
                using (stream = File.Open(filename, FileMode.OpenOrCreate, FileAccess.Write))
                {
                    bFormatter = new BinaryFormatter();
                    bFormatter.Serialize(stream, objectToSerialize);
                    stream.Flush();
                }
            }
            catch (Exception e)
            {
                System.Windows.Forms.MessageBox.Show(e.Message);
            }
            finally
            {
                if (stream != null) stream.Close(); stream.Dispose();

            }

        }

        static public Type Load<Type>(string filename)
        {
            Type objectToSerialize;
            Stream stream = File.Open(filename, FileMode.Open);
            BinaryFormatter bFormatter = new BinaryFormatter();
            objectToSerialize = (Type)bFormatter.Deserialize(stream);
            stream.Close();
            return (Type)objectToSerialize;
        }

    }
}
